{
Function:       _OrderExecuted

Description:    Determine which order was just executed in a strategy.

                 Requires Strategy Properties for All Strategies - Automation to be formatted to:
                     1. "Adopt the real-world position for the current account.
                     2. "Wait for a UROut confirmation before sending a subsequent entry order for the same signal to the market.

Author:         MarkSanDiego

Updated:        10/02/10 original version
    
Usage: 
__________________________________________________________________________________________________________________
    
inputs:
 MaxLimitReversals(0),
 MaxStopReversals(0),
 DebugOK(false);
 
vars:
    intrabarpersist MP1(0),                 { market position of previous tick }
    intrabarpersist MP(0),                  { market position variable } 
 
    intrabarpersist BuyLimitValue(0),       { value of BuyLimit for current bar }     
    intrabarpersist BuyStopValue(0),        { value of BuyStop for current bar }
    intrabarpersist SellLimitValue(0),      { value of SellLimit for current bar }
    intrabarpersist SellStopValue(0),       { value of SellStop for current bar }
    
    intrabarpersist BuyLimitValue2(0),      { value of BuyLimit for current bar }
    intrabarpersist BuyStopValue2(0),       { value of BuyStop for current bar }
    intrabarpersist SellLimitValue2(0),     { value of SellLimit for current bar }
    intrabarpersist SellStopValue2(0),      { value of SellStop for current bar }
    
    ReverseOnLimitOK(ReverseOnLimit),
    ReverseOnStopOK(StopReversals < MaxStopReversals),
    Reversal(false),
    
{ trade reversal variables } { facilitates reversals when strategy formatted for "Wait for an UROut confirmation" }

    Intrabarpersist ReverseOnLimitOK(MaxLimitReversals > 0),
    Intrabarpersist ReverseOnStopOK(MaxStopReversals > 0),
    
    Intrabarpersist StopReversals(0),        { number of reversals occuring with stop orders }
    Intrabarpersist Reversal(false),         { true if a trade reversal has occurred }
    BuyLimitValue2(0),
    BuyStopValue2(0),
    SellLimitValue2(0),
    SellStopValue2(0);

arrays:
    Intrabarpersist Orders[50](0);     { Count of each order type. Orders[ID_BuyLimit] is total count of BuyLimit orders.
                                         ID_BuyLimit is the ID number of the trend line used for BuyLimitValue orders.
                                         Since the elements of Orders[] are the trend line ID numbers, the dimension of this
                                         array must be large enough to accomodate the highest trend line ID number that might be
                                         generated by user drawn trend lines. If user continues to add and delete trend lines
                                         on the chart as time goes by, ID numbers much higher than 1-4 may be generated by the four
                                         basic order types. Therefore, The dimension of Orders[] must be significantly higher 
                                         than four. A value of 50 is chosen as this value is unlikely to ever be exceeded in
                                         normal use. }



    { zero out inactive limit and stop values }
        BuyLimitValue2 = iff(BuyLimitOK, BuyLimitValue, 0);
        BuyStopValue2 = iff(BuyStopOK, BuyStopValue, 0);
        SellLimitValue2 = iff(SellLimitOK, SellLimitValue, 0);
        SellStopValue2 = iff(SellStopOK, SellStopValue, 0);
            
    { determine if any orders were executed since prior tick }    
                                        
        ID_OrderExecuted = _OrderExecuted(MP1, MP, ID_BuyLimit, ID_BuyStop, ID_SellLimit, ID_SellStop,
            BuyLimitValue2, BuyStopValue2, SellLimitValue2, SellStopValue2, 
            ReverseOnLimitOK, ReverseOnStopOK, DebugOK, Reversal, Orders);     

__________________________________________________________________________________________________________________    
}

inputs: 
     MP1(NumericRef), { market position of prior tick }
     MP(NumericRef), { current market position variable }
 
     ID_BuyLimit(NumericRef),
     ID_BuyStop(NumericRef),
     ID_SellLimit(NumericRef),
     ID_SellStop(NumericRef),
        
     BuyLimitValue(NumericRef),
     BuyStopValue(NumericRef),
     SellLimitValue(NumericRef),
     SellStopValue(NumericRef), 
        
     ReverseOnLimitOK(TrueFalseRef),
     ReverseOnStopOK(TrueFalseRef),
     DebugOK(TrueFalseSimple),
 
{ output variables and arrays }
     Reversal(TrueFalseRef),
     Orders[xx](NumericArrayRef);
 

vars:
    Intrabarpersist ID_Order(0); { ID number corresponding to order type (Buy Limit, Buy Stop, etc.) }
       


 { determine if any stops were hit this bar, starting 1 bar AFTER FirstStrategyBar }
 
    switch MP1 begin { MP1 is market position during prior tick }
 
        Case 0: { test for entry order } 
 
             Switch MP begin
 
                 Case 0: { no action }
                      ID_Order = 0;
 
                 Case 1: { Buy Limit or Buy Stop }
                     ID_Order = iff(absvalue(c - BuyLimitValue) < Absvalue(c - BuyStopValue), ID_BuyLimit, ID_BuyStop);
 
                 Case -1: { Sell Limit or Sell Stop } 
                     ID_Order = iff(absvalue(c - SellLimitValue) < Absvalue(c - SellStopValue), ID_SellLimit, ID_SellStop);
 
              end;
     
               
        Case 1:
 
            Switch MP begin
 
                Case 1: { no action }
                    ID_Order = 0;
 
                Case 0: { Sell Limit or Sell Stop } 
                    ID_Order = iff(absvalue(c - SellLimitValue) < Absvalue(c - SellStopValue), ID_SellLimit, ID_SellStop); 
                    Reversal = (ID_Order = ID_SellLimit and ReverseOnLimitOK) or 
                        (ID_Order = ID_SellStop and ReverseOnStopOK); 
 
                 Case -1: { Sell Limit Reversal or Sell Stop Reversal completed }
                     ID_Order = iff(absvalue(c - SellLimitValue) < Absvalue(c - SellStopValue), ID_SellLimit, ID_SellStop); 
                     Reversal = true;
 
            end;
            
            
        Case -1:
 
            Switch MP begin
 
                Case -1: { no action }
                    ID_Order = 0;
 
                Case 0: { Buy Limit or Buy Stop } 
                    ID_Order = iff(absvalue(c - BuyLimitValue) < Absvalue(c - BuyStopValue), ID_BuyLimit, ID_BuyStop);
                    Reversal = (ID_Order = ID_BuyLimit and ReverseOnLimitOK) or 
                        (ID_Order = ID_BuyStop and ReverseOnStopOK);
 
                Case 1: { Buy Limit Reversal or Buy Stop Reversal completed } 
                    ID_Order = iff(absvalue(c - BuyLimitValue) < Absvalue(c - BuyStopValue), ID_BuyLimit, ID_BuyStop);
                    Reversal = true;
 
            end;
 
    end; 

 
        { Debug - Display Orders }
            If DebugOK then begin
 
               Switch ID_Order begin
 
                   Case 0:
                       value1 = 0; { no order executed }
                   Case ID_BuyLimit:
                       print(_DateTimeSymbol, "******** BuyLimit executed ******** ");
                   Case ID_BuyStop:
                       print(_DateTimeSymbol, "******** BuyStop executed ******** ");
                   Case ID_SellLimit:
                       print(_DateTimeSymbol, "******** SellLimit executed ******** "); 
                   Case ID_SellStop:
                       print(_DateTimeSymbol, "******** SellStop executed ******** ");
               
               end;
 
              If Reversal then print(_DateTimeSymbol, "******** REVERSAL ******** ");
 
          end;
                
                
        if ID_Order > 0 then Orders[ID_Order] = Orders[ID_Order] + 1;
 
        _OrderExecuted = ID_Order;